package com.wisdomparking.control.app;

import java.io.File;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.wisdomparking.bean.User;
import com.wisdomparking.config.mvc.BaseController;
import com.wisdomparking.config.mvc.Result;
import com.wisdomparking.config.mvc.ResultGenerator;
import com.wisdomparking.constant.Constants;
import com.wisdomparking.service.UserService;
import com.wisdomparking.tools.MD5;
import com.wisdomparking.tools.ToolFile;
import com.wisdomparking.tools.ToolString;
import com.wisdomparking.tools.ToolValid;

/**
 * 用户中心
 */
@RestController
@RequestMapping("/app/userCenter")
public class UserController extends BaseController{
	
	@Autowired UserService userService;
	
	/**
	 * 获取手机验证码
	 * 
	 * @param mobileNumber
	 * @return
	 */
	@RequestMapping("/getIdentifyingCode")
	public Result<String> getIdentifyingCode(String mobileNumber){
		String code = ToolString.generateIdentifyingCode();
		boolean flag = true;	// ToolsSMS.sendMobileMsg(mobileNumber, code);
		User user = null;
		User _user = null;
		if(flag){
			user = userService.getUserByMobileNumber(mobileNumber);
			_user = userService.getUserByTempMobile(mobileNumber);
			
			if(ToolValid.isNull(user) && ToolValid.isNull(_user)){
				user = new User();
				user.setTempMobile(mobileNumber);
				user.setIdentifyingCode(code);
				user.setCreateTime(new Date());
				
				userService.addUser(user, true);
			}
			else{
				if(ToolValid.isNotNull(_user)){
					user = _user;
				}
				user.setIdentifyingCode(code);
				
				flag = userService.updateUser(user, true);
			}
			
		}
		
		return ResultGenerator.genSuccessResult(code);
	}
	
	/**
	 * 手机号是否被注册
	 * @param mobileNumber
	 * @return
	 */
	@RequestMapping("/getReMobileNumber")
	public Result<Boolean> getReMobileNumber(String mobileNumber){
		boolean flag = false;
		User user = userService.getUserByMobileNumber(mobileNumber);
		flag = ToolValid.isNull(user)?false:true;
		
		return ResultGenerator.genSuccessResult(flag);
	}
	
	/**
	 * 用户注册
	 * @param user
	 * @return
	 */
	@RequestMapping("/register")
	public Result<User> register(User user){
		if(ToolValid.isNotNull(user)){
			User _user = userService.getUserByMobileNumber(user.getMobileNumber());
			if(ToolValid.isNull(_user)){
				User u = userService.getUserByTempMobile(user.getMobileNumber());
				if(ToolValid.isNotNull(u) && u.getIdentifyingCode().equals(user.getIdentifyingCode())){
					user.setId(u.getId());
					user.setPassword(MD5.getMD5Str(user.getPassword()));
					user.setCreateTime(u.getCreateTime());
					user.setIdentifyingCode("");
					
					userService.updateUser(user, true);
				}
			}
		}
		
		return ResultGenerator.genSuccessResult(user);
	}
	
	
	/**
	 * 上传用户头像
	 * 
	 * @param file
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/uploadHeadPortrait", method = RequestMethod.POST)
	public Result<User> uploadImg(String mobileNumber, MultipartFile file, HttpServletRequest request) {
		User user = null;
		try {
			String folder = "imgupload" + File.separator + "head" + File.separator;
			String fileName = file.getOriginalFilename();
			String filePath = request.getSession().getServletContext().getRealPath(folder);
			
			ToolFile.uploadFile(file.getBytes(), filePath, mobileNumber + Constants.SPLIT_STRIPING + fileName);
			String src = folder + mobileNumber + Constants.SPLIT_STRIPING + fileName;

			user = userService.getUserByMobileNumber(mobileNumber);
			user.setHeadPortrait(src);

			userService.updateUser(user, false);

		} 
		catch (Exception e) {
			e.printStackTrace();
		}

		return ResultGenerator.genSuccessResult(user);
	}
	
	/**
	 * 找回密码-验证码验证
	 * 
	 * @param mobileNumber
	 * @param identifyingCode
	 * @return
	 */
	@RequestMapping(value = "/validIdentifyingCode", method = RequestMethod.POST)
	public Result<Boolean> validIdentifyingCode(String mobileNumber, String identifyingCode){
		boolean flag = false;
		
		User user = userService.getUserByMobileNumber(mobileNumber);
		if(ToolValid.isNotNull(user)){
			if(identifyingCode.equals(user.getIdentifyingCode())){
				flag = true;
			}
		}
		
		return ResultGenerator.genSuccessResult(flag);
	}
	
	
	/**
	 * 找回密码-设置新密码
	 * 
	 * @param mobileNumber
	 * @param newpassword
	 * @return
	 */
	@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
	public Result<Boolean> updatePassword(String mobileNumber, String newpassword, String identifyingCode){
		boolean flag = false;
		User user = userService.getUserByMobileNumber(mobileNumber);
		
		if(ToolValid.isNotNull(user)){
			if(identifyingCode.equals(user.getIdentifyingCode())) {
				user.setPassword(MD5.getMD5Str(newpassword));
				user.setIdentifyingCode("");
				flag = userService.updateUser(user, true);
			}
		}
		
		return ResultGenerator.genSuccessResult(flag);
	}
	
	
	/**
	 * App 登录
	 * 
	 * @param userName
	 * @param password
	 * @return
	 */
	@RequestMapping("/login")
	public Result<User> managerLogin(String mobileNumber, String password){
		User user = userService.getUserByMobileNumber(mobileNumber);
		
		if(ToolValid.isNotNull(user)){
			if(!user.getPassword().equals(MD5.getMD5Str(password))){
				user = null;
			}
		}
		else {
			user = null;
		}
		return ResultGenerator.genSuccessResult(user);
	}
	
	
	/**
	 * 获取用户信息
	 * @param mobileNumber
	 * @return
	 */
	@RequestMapping(value = "/getUser", method = RequestMethod.POST)
	public Result<User> getUser(String mobileNumber){
		User user = userService.getUserByMobileNumber(mobileNumber);
		return ResultGenerator.genSuccessResult(user);
	}
	
	
	/**
	 * 设置默认支付方式
	 * @param mobileNumber
	 * @param defaultPay
	 * @return
	 */
	@RequestMapping(value = "/setDefaultPay")
	public Result<Boolean> setDefaultPay(String mobileNumber, String defaultPay){
		boolean flag = false;
		
		User user = userService.getUserByMobileNumber(mobileNumber);
		if (ToolValid.isNotNull(user)) {
			if (("wechat".equals(defaultPay) && ToolValid.isNotNull(user.getWeChatAuthorize()))
					|| (("alipay".equals(defaultPay) && ToolValid.isNotNull(user.getAlipayAuthorize())))) {
				user.setDefaultPay(defaultPay);
				flag = userService.updateUser(user, true);
			}
		}
		
		return ResultGenerator.genSuccessResult(flag);
	}
	
	/**
	 * 支付方式授权
	 * @param mobileNumber
	 * @param defaultPay
	 * @return
	 */
	@RequestMapping(value = "/payAuthorize")
	public Result<Boolean> payAuthorize(String mobileNumber, String defaultPay){
		boolean flag = false;
		User user = userService.getUserByMobileNumber(mobileNumber);
		if (ToolValid.isNotNull(user)) {
			
			if("wechat".equals(defaultPay)) {
				user.setWeChatAuthorize(true);
			}
			
			if("alipay".equals(defaultPay)) {
				user.setAlipayAuthorize(true);
			}
			
			flag = userService.updateUser(user, true);
		}
		return ResultGenerator.genSuccessResult(flag);
	}
	
}
